package com.crawler.harbour.utils;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.crawler.harbour.utils.DateUtils;

public class StorageDay {
	
	/**
	 * 获取堆存天数
	 * @param inDate 进场日期
	 * @param outDate 出场日期
	 * @return 
	 */
	 public String computeStorageDays(String inDate, String outDate) {

	        if (inDate == null)
	            return "0";
	        if (outDate == null)
	            outDate = String.valueOf(new Date().getTime());

	        String patternStr = "\\d+";
	        Pattern pattern = Pattern.compile(patternStr);
	        Matcher indayMatch = pattern.matcher(inDate);
	        Matcher outdayMatch = pattern.matcher(outDate);

	        String inDayTimestamp = null;
	        String outDayTimestamp = null;
	        if (indayMatch.find()) {
	            String dateStr = indayMatch.group(0);
	            if (dateStr.length() == 13) {
	                inDayTimestamp = dateStr;
	            } else {
	                return "0";
	            }
	        }
	        if (outdayMatch.find()) {
	            String dateStr = outdayMatch.group(0);
	            if (dateStr.length() == 13) {
	                outDayTimestamp = dateStr;
	            } else {
	                return "0";
	            }
	        }

	        if (inDayTimestamp == null)
	            return "0";

	        String inDateStr = DateUtils.dateToStr(new Date(Long.valueOf(inDayTimestamp)));

	        String outDateStr = outDayTimestamp == null ? DateUtils.getStringDateShort()
	                : DateUtils.dateToStr(new Date(Long.valueOf(outDayTimestamp)));

	        //2017-12-12 ~ 2017-12-12 算1天
	        int days = Integer.valueOf(DateUtils.getTwoDay(outDateStr, inDateStr)) + 1;

	        return String.valueOf(days);
	    }

}
